import view from './view';
var events = require('events');
var template = require('template');
var request = require('{pub}/request');
var placeholder = require('placeholder');
var func = require('{pub}/function');
var codeUrl = serviceUrl+'/login/check.do';
return events({
    element:$('#container'),
    init:function() {
        this.render()
    },
    events:{
        'click .img_validator':'updateCode',
        'click #submit':'loginIn'
    },
    loginIn:function(e,elem) {//登录
        var username = $('#username').val();
        var password = $('#password').val();
        var validator = $('#validator').val();
        var param = {
            username:username,
            password:password,
            vcheckcode:validator
        }
        console.log(param)
        this.reqLogin(param)
    },
    updateCode:function(e,elem) {
        var othis = elem || $('.img_validator');
        var ncodeUrl = codeUrl +'?_='+ Date.parse(new Date());
        othis.attr('src',ncodeUrl)
    },
    reqLogin:function(param) {
        var that = this;
        request.post('/login/userLogin',param,function(res){
            if(res.result === 'success'){
                var data = res.data
                var userInfo = data && {
                   cPhone: data.cPhone,
                   cRealname: data.cRealname,
                   cUsername: data.cUsername,
                   cUsertype: data.cUsertype,
                }
                var menuList = data.menuList;
                // var deptInfoList = data.deptInfoList;
                func.setCookie('adminUser',(userInfo));
                func.setCookie('menuList',(menuList));
                // func.setCookie('deptInfoList',(deptInfoList));
                func.location('/platform/index.html')
            } else if(res.result === 'fail') {
                that.updateCode();
            }
        })
    },
    render:function() {
        var that = this;
        that.element.html(template.render(view,{codeUrl:codeUrl}));
        placeholder.init(that.element);
    }
})